package teacher.clazz.student.daoImple;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import teacher.clazz.student.dao.CSTdao;
import teacher.clazz.student.entities.Student;

@Repository
// dao层标示符
public class CSTdaoImpl implements CSTdao {

	@Autowired
	// 自动注入
	private SessionFactory sessionFactory;

	// 获取当前线程
	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	// 通过老师id来查找学生信息
	public List<Student> selectByTeacherId(Integer teacherId) {

		String sql = "select student.* from student join clazz on  student.CLAZZID=clazz.id join teacher on clazz.id=teacher.id where teacher.id=?";

		List<Student> students = getSession().createSQLQuery(sql)
				.addEntity(Student.class).setInteger(0, teacherId).list();

		for (Student student : students) {
			System.out.println(student);
		}
		return students;
	}

	@Override
	// 根据学生id来查找学生信息
	public List<Student> selectByStudentId(Integer studentId) {

		String sql = "select * from student where id=?";
		List<Student> students = getSession().createSQLQuery(sql)
				.addEntity(Student.class).setInteger(0, studentId).list();

		return students;
	}

	@Override
	// 根据学生id来删除学生
	public Boolean deleteBystudentid(Integer  studentId) {

		String sql = "delete from student where id=?";
		
		Integer integer=getSession().createSQLQuery(sql).setInteger(0, studentId)
					.executeUpdate();//是否修改了数据
			
		if(integer>0){
			
			return true;
		}

		return false;
	}

	@Override
	//查询学生所有的id
	public List<Integer> selectId() {
		String sql="select id from student";
	List<Integer> integers=	getSession().createSQLQuery(sql).addEntity(Student.class).list();
		
		return integers;
	}
	
	

}
